<template>
  <div class="login-container">
    <div class="login-wrapper">
      <div class="login-left">
        <img src="../assets/imgs/学习.png" class="login-image">
        <div class="image-overlay">
          <h2>欢迎来到博客平台</h2>
          <p>分享知识，记录生活</p>
        </div>
      </div>

      <div class="login-right">
          <div class="login-header">
            <h1>博客平台登录</h1>
            <p>请输入您的账号信息</p>
          </div>
          <el-form :model="form" :rules="rules" ref="formRef" class="login-form">
            <el-form-item prop="username">
              <el-input 
                size="large" 
                prefix-icon="el-icon-user" 
                placeholder="请输入账号" 
                v-model="form.username"
                class="login-input"
              ></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input 
                size="large" 
                prefix-icon="el-icon-lock" 
                placeholder="请输入密码" 
                show-password
                v-model="form.password"
                class="login-input"
              ></el-input>
            </el-form-item>
            <el-form-item prop="code">
              <div class="code-input-wrapper">
                <el-input  
                  prefix-icon="el-icon-circle-check"
                  size="large" 
                  v-model="code" 
                  placeholder="请输入验证码"
                  class="login-input code-input"
                ></el-input>
                <Identify :identifyCode="identifyCode" @click.native="refreshCode" class="verify-code"/>
              </div>
            </el-form-item>
            <el-form-item prop="role">
              <el-radio-group v-model="form.role" class="role-group">
                <el-radio label="ADMIN" class="role-radio">管理员</el-radio>
                <el-radio label="USER" class="role-radio">用户</el-radio>
              </el-radio-group>
            </el-form-item>

            <el-form-item>
              <el-button 
                size="large"
                class="login-btn"
                @click="login"
              >
                <i class="el-icon-right"></i>
                登 录
              </el-button>
            </el-form-item>
            <div class="register-link">
              <span>还没有账号？</span>
              <a href="/register" class="register-btn">立即注册</a>
            </div>
          </el-form>
      </div>

    </div>

  </div>
</template>

<style scoped>
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-wrapper {
  display: flex;
  width: 900px;
  min-height: 500px;
  max-height: 90vh;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.login-left {
  flex: 1;
  position: relative;
  overflow: hidden;
}

.login-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.login-left:hover .login-image {
  transform: scale(1.05);
}

.image-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(45deg, rgba(102, 126, 234, 0.8), rgba(118, 75, 162, 0.8));
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: white;
  text-align: center;
  padding: 40px;
}

.image-overlay h2 {
  font-size: 28px;
  margin-bottom: 16px;
  font-weight: 600;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.image-overlay p {
  font-size: 16px;
  opacity: 0.9;
  margin: 0;
}

.login-right {
  flex: 1;
  padding: 30px 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  overflow-y: auto;
  max-height: 100%;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h1 {
  font-size: 24px;
  color: #2c3e50;
  margin-bottom: 8px;
  font-weight: 600;
  line-height: 1.2;
}

.login-header p {
  color: #666;
  font-size: 14px;
  margin: 0;
}

.login-form {
  width: 100%;
}

.login-input {
  margin-bottom: 16px;
}

.login-input .el-input__inner {
  height: 50px !important;
  border-radius: 12px !important;
  border: 2px solid #e1e8ed !important;
  font-size: 14px !important;
  transition: all 0.3s ease !important;
  background: rgba(255, 255, 255, 0.8) !important;
}

.login-input .el-input__inner:focus {
  border-color: #667eea !important;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1) !important;
}

.code-input-wrapper {
  display: flex;
  gap: 12px;
  align-items: center;
}

.code-input {
  flex: 1;
  margin-bottom: 0 !important;
}

.verify-code {
  height: 50px;
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.3s ease;
}

.verify-code:hover {
  transform: scale(1.05);
}

.role-group {
  display: flex;
  justify-content: center;
  gap: 30px;
  margin: 16px 0;
}

.role-radio {
  font-size: 14px;
  color: #666;
}

.role-radio.is-checked {
  color: #667eea;
}

.login-btn {
  width: 100% !important;
  height: 50px !important;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
  border: none !important;
  border-radius: 12px !important;
  font-size: 16px !important;
  font-weight: 600 !important;
  color: white !important;
  transition: all 0.3s ease !important;
  margin-top: 16px;
}

.login-btn:hover {
  transform: translateY(-2px) !important;
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4) !important;
}

.login-btn:active {
  transform: translateY(0) !important;
}

.register-link {
  text-align: center;
  margin-top: 16px;
  color: #666;
  font-size: 14px;
}

.register-btn {
  color: #667eea;
  text-decoration: none;
  font-weight: 600;
  margin-left: 8px;
  transition: all 0.3s ease;
}

.register-btn:hover {
  color: #764ba2;
  text-decoration: underline;
}

@media (max-width: 768px) {
  .login-wrapper {
    flex-direction: column;
    width: 90%;
    height: auto;
  }
  
  .login-left {
    height: 200px;
  }
  
  .login-right {
    padding: 30px 20px;
  }
}
</style>

<script>
import Identify from "@/components/Identify";

export default {
  name: "Login",
  components: {
    Identify
  },
  data() {
    return {
      form: {role: 'ADMIN'},
      rules: {
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ]
      },
      code: '',   // 表单绑定的验证码
      // 图片验证码
      identifyCode: '',
      // 验证码规则
      identifyCodes: '123456789ABCDEFGHGKMNPQRSTUVWXY',
    }
  },
  mounted() {
    this.refreshCode()
  },
  methods: {
    // 切换验证码
    refreshCode() {
      this.identifyCode = ''
      this.makeCode(this.identifyCodes, 4)
    },
    // 生成随机验证码
    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode += this.identifyCodes[Math.floor(Math.random() * (this.identifyCodes.length))]
      }
    },
    login() {
      if (!this.code) {
        this.$message.warning('请输入验证码')
        this.refreshCode()
        return
      }
      if (this.code.toLowerCase() !== this.identifyCode.toLowerCase()) {
        this.$message.warning('验证码错误')
        this.refreshCode()
        return
      }
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          // 验证通过
          this.$request.post('/login', this.form).then(res => {
            if (res.code === '200') {
              localStorage.setItem("xm-user", JSON.stringify(res.data))  // 存储用户数据
              this.$message.success('登录成功')
              setTimeout(() => {
                // 跳转主页
                if (res.data.role === 'ADMIN') {
                  location.href = '/home'
                } else {
                  location.href = '/front/home'
                }
              }, 500)
            } else {
              this.refreshCode()
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.container {
  height: 100vh;
  overflow: hidden;
  background-color: #05634c;
}

a {
  color: #05634c;
}
</style>
